package com.croofr.utils.file;

import com.croofr.utils.string.StringUtils;

import java.io.File;

/**
 * Utility for File.
 * <p/>
 * User: bednar<br/>
 * Date: Jan 17, 2010<br/>
 * Time: 12:30:19 PM
 */
public final class FileUtils
{

    /**
     * Return true if file on specified path exist.
     * If filePath is directory than return true.
     * If filePath is empty than return false.
     *
     * @param filePath path to file or directory
     * @return true if file exist
     */
    public static boolean exist(String filePath)
    {
        if (StringUtils.isEmpty(filePath))
        {
            return false;
        }

        File file = new File(filePath);

        return file.exists();
    }

    /**
     * Return true if file on specified path not exist.
     * If filePath is directory than return false.
     * If filePath is empty than return true.
     *
     * @param filePath path to file or directory
     * @return true if file doesnt exist or filePath is empty
     */
    public static boolean notExist(String filePath)
    {
        return !exist(filePath);
    }

    /**
     * Return true id fiel on dirPath is directory.
     * If dirPath is empty than return false.
     *
     * @param dirPath path to directory
     * @return true if file on path is directory
     */
    public static boolean isDirectory(String dirPath)
    {
        if (notExist(dirPath))
        {
            return false;
        }

        File file = new File(dirPath);

        return file.isDirectory();
    }

    /**
     * Return true if file on dirPath doesnt exist or if is "pure" file.
     * If dirPath is empty than return true.
     *
     * @param dirPath path to dir
     * @return true id file on dirPath isnt directory.
     */
    public static boolean isNotDirectory(String dirPath)
    {
        return !isDirectory(dirPath);
    }

    /**
     * Return true if file on filePath is "pure" file.
     * If file on filePath doesnt exist than return false.
     *
     * @param filePath path to file
     * @return true if file on filePath is "pure" file.
     */
    public static boolean isFile(String filePath)
    {
        //noinspection SimplifiableIfStatement
        if (notExist(filePath))
        {
            return false;
        }

        return isNotDirectory(filePath);
    }

    /**
     * Return true if file on path doesnt exist. Or if file
     * on filePath is directory.
     *
     * @param filePath path to file
     * @return true id file on filePath doesnt exist
     */
    public static boolean isNotFile(String filePath)
    {
        return !isFile(filePath);
    }
}
